<template>
  <div class="app-container">
    <div class="header">
      <div class="title">数据可视化</div>
      <div class="timer">{{ timer }}</div>
    </div>
    <div class="content">
      <div class="left border">
        <div class="left-top borderb">
          <demo :options="option1" />
        </div>
        <div class="left-center borderb">
          <demo :options="option2" />
          <span class="total">能耗总数：100</span>
        </div>
        <div class="left-bottom borderb">
          <el-table
            ref="tableRef"
            :data="list1"
            class="myTable"
            height="270px"
            style="width: 580px"
          >
            <el-table-column
              v-for="item in list1C"
              :key="item.prop"
              :label="item.label"
              :prop="item.prop"
            ></el-table-column>
          </el-table>
        </div>
      </div>
      <div class="center border borderb">
        <div class="center-top borderb">
          <el-row>
            <el-col :span="8">
              <div class="box">
                <div class="label">今年累计销售额</div>
                <count-to
                  :start-val="0"
                  :end-val="list2.yearSales"
                  :duration="2600"
                  class="card-panel-num"
                  :decimals="2"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="box">
                <div class="label">本月累计销售额</div>
                <count-to
                  :start-val="0"
                  :end-val="list2.monthSales"
                  :duration="2600"
                  class="card-panel-num"
                  :decimals="2"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="box">
                <div class="label">今日累计销售额</div>
                <count-to
                  :start-val="0"
                  :end-val="list2.daySales"
                  :duration="2600"
                  class="card-panel-num"
                  :decimals="2"
                />
              </div>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <div class="box">
                <div class="label">本年累计碳排放</div>
                <count-to
                  :start-val="0"
                  :end-val="list2.dayEmission"
                  :duration="2600"
                  class="card-panel-num"
                  :decimals="2"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="box">
                <div class="label">本月累计碳排放</div>
                <count-to
                  :start-val="0"
                  :end-val="list2.monthEmission"
                  :duration="2600"
                  class="card-panel-num"
                  :decimals="2"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="box">
                <div class="label">今日累计碳排放</div>
                <count-to
                  :start-val="0"
                  :end-val="list2.yearEmission"
                  :duration="2600"
                  class="card-panel-num"
                  :decimals="2"
                />
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="center-center borderb">
          <el-table :data="list1" class="myTable2" height="330px">
            <el-table-column
              v-for="item in list2C"
              :key="item.prop"
              :label="item.label"
              :prop="item.prop"
            ></el-table-column>
          </el-table>
        </div>
        <div class="center-bottom borderb">
          <demo :options="option3" />
        </div>
      </div>
      <div class="right border">
        <div class="right-top borderb">
          <demo :options="option4" />
        </div>
        <div class="right-center borderb">
          <demo :options="option5" />
        </div>
        <div class="right-bottom borderb">
          <el-table
            ref="tableRef"
            :data="list1"
            class="myTable"
            height="270px"
            style="width: 580px"
          >
            <el-table-column
              v-for="item in list2C"
              :key="item.prop"
              :label="item.label"
              :prop="item.prop"
            ></el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import demo from './component/demo.vue'
import scrollTable from './component/scroll-table.vue'
import { getList1, getList2, getList3 } from '@/api_zc/echarts/index'
import CountTo from 'vue-count-to'
export default {
  data () {
    return {
      timer: new Intl.DateTimeFormat('zh-CN', { dateStyle: 'full', timeStyle: 'medium' }).format(new Date()),
      list1: [],
      list2: {},
      list3: [],
      list1C: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '物料',
          prop: 'material.id'
        },
        {
          label: '物料名称',
          prop: 'material.name'
        },
        {
          label: '仓库',
          prop: 'warehouse.id'
        },
        {
          label: '仓库名称',
          prop: 'warehouse.name'
        },
        {
          label: '当前库存',
          prop: 'stock'
        },
        {
          label: '库存数量',
          prop: 'totalStock'
        },
        {
          label: '状态',
          prop: 'status'
        },
      ],
      list2C: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '设备',
          prop: 'material.name'
        },

        {
          label: '生产数量',
          prop: 'stock'
        },
        {
          label: '碳排放',
          prop: 'totalStock'
        },
      ],
      option1: {
        series: [
          {
            type: 'gauge',
            name: '耗电量',
            radius: '65%',
            center: ['20%', '50%'],
            detail: { formatter: '{value} kWh', fontSize: 14 },
            data: [{ value: 80, name: '耗电量' }],
            axisLine: {
              lineStyle: {
                width: 10
              }
            }
          },
          {
            type: 'gauge',
            name: '耗水量',
            radius: '65%',
            center: ['50%', '50%'],
            detail: { formatter: '{value} m³', fontSize: 14 },
            data: [{ value: 70, name: '耗水量' }],
            axisLine: {
              lineStyle: {
                width: 10
              }
            }
          },
          {
            type: 'gauge',
            name: '碳排放量',
            radius: '65%',
            center: ['80%', '50%'],
            detail: { formatter: '{value} tCO2', fontSize: 14 },
            data: [{ value: 50, name: '碳排放量' }],
            axisLine: {
              lineStyle: {
                width: 10
              }
            }
          },
        ]
      },
      option2: {
        series: [
          {
            type: "pie",
            radius: ['70%', '85%'],
            data: [
              {
                name: '办公耗电量',
                value: '150'
              },
              {
                name: '办公用水量',
                value: '100'
              },
              {
                name: '生产用水量',
                value: '400'
              },
              {
                name: '生产耗电量',
                value: '200'
              },
            ]
          },
        ]
      },
      option3: {
        xAxis: {
          type: 'category',
          data: ['2021-11', '2021-12', '2022-1', '2022-2', '2022-3', '2022-4', '2022-5']
        },
        yAxis: [
          {
            type: 'value',
            name: '销售数量',
            min: 0,
            max: 300,
            interval: 10,
            axisLabel: {
              formatter: '{value} '
            }
          },
          {
            type: 'value',
            name: '销售计划',
            min: 0,
            max: 100,
            interval: 10,
            axisLabel: {
              formatter: '{value}% '
            }
          },
        ],
        legend: {

        },
        series: [
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: 'bar',
            name: '已销售'
          },
          {
            data: [150, 230, 224, 218, 135, 147, 260],
            type: 'bar',
            name: '销售计划'
          },
          {
            data: [10, 50, 60, 70, 50, 60, 50],
            type: 'line',
            name: '销售计划',
            yAxisIndex: 1,
          }
        ]
      },
      option4: {
        xAxis: {
          type: 'category',
          data: []
        },
        yAxis: {
          type: 'value',
        },
        legend: {
        },
        series: [
          {
            data: [],
            type: 'line',
            smooth: true,
            name: '合同额'
          },
          {
            data: [],
            type: 'line',
            name: '总销售额',
            smooth: true,
          },
        ]
      },
      option5: {
        xAxis: {
          type: 'value'
        },
        yAxis: {
          type: 'category',
          data: ['公司a', '公司b', '公司c', '公司d', '公司e', '公司f', '公司g', '公司h']
        },
        series: [
          {
            data: [
              { value: 182, itemStyle: { color: '#FF6A6A' } },
              { value: 239, itemStyle: { color: '#FFB74D' } },
              { value: 294, itemStyle: { color: '#FFE082' } },
              { value: 100, itemStyle: { color: '#C5E1A5' } },
              { value: 144, itemStyle: { color: '#81C784' } },
              { value: 630, itemStyle: { color: '#4FC3F7' } },
              { value: 630, itemStyle: { color: '#CEFFCE' } },
              { value: 630, itemStyle: { color: '#977C00' } }
            ].sort((a, b) => a.value - b.value),
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.2)'
            },
            label: {
              show: true,
              position: ['100%', '50%']
            }
          }
        ]
      }
    }
  },
  mounted () {
    this.getList1()
    this.getList2()
    this.getList3()
    setInterval(() => {
      this.timer = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'full', timeStyle: 'medium' }).format(new Date())
    }, 1000)
    this.scroll(this.$refs.tableRef)
  },
  methods: {
    async getList1 () {
      const { data } = await getList1()
      this.list1 = data.sort((a, b) => {
        return b.stock - a.stock
      })
    },
    async getList2 () {
      const { data } = await getList2()
      this.list2 = data
    },
    async getList3 () {
      const { data } = await getList3()
      let xData = data.contract.map(item => item.month)
      let yData1 = data.contract.map(item => item.amount)
      let yData2 = data.order.map(item => item.amount)
      this.option4.xAxis.data = xData
      this.option4.series[0].data = yData1
      this.option4.series[1].data = yData2
    },
    scroll (ref) {
      requestAnimationFrame(function scroll () {
        ref.bodyWrapper.scrollTop += 1
        if (ref.bodyWrapper.scrollTop + ref.bodyWrapper.clientHeight == ref.bodyWrapper.scrollHeight) {
          ref.bodyWrapper.scrollTop = 0
        }
        requestAnimationFrame(scroll)
      })
    }
  },
  components: {
    demo,
    scrollTable,
    CountTo
  }
}
</script>

<style lang="scss">
.myTable ::-webkit-scrollbar {
  width: 0px;
  height: 10px;
}
.myTable ::-webkit-scrollbar-thumb {
  background-color: rgb(238, 238, 238);
}
.myTable2 ::-webkit-scrollbar {
  width: 10px;
}
.myTable2 ::-webkit-scrollbar-thumb {
  background-color: rgb(238, 238, 238);
}
.app-container {
  background-color: #eee;
  height: calc(100vh - 85px);
  position: relative;
  display: flex;
  flex-direction: column;
  .header {
    height: 40px;
    border-bottom: 1px dotted #000;
    .title {
      font-size: 24px;
      font-weight: 500;
      text-align: center;
    }
    .timer {
      position: absolute;
      right: 50px;
      top: 20px;
    }
  }
  .content {
    flex: 1;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    border-left: 1px dotted #000;
    .left {
      display: grid;
      grid-template-rows: 1fr 1fr 1fr;
    }
    .center {
      display: grid;
      grid-template-rows: 150px 1fr 1fr;
    }
    .right {
      display: grid;
      grid-template-rows: 1fr 1fr 1fr;
    }
  }
}
.border {
  border-right: 1px dotted #000;
}
.borderb {
  border-bottom: 1px dotted #000;
}
.left-center {
  position: relative;
  .total {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50px, -9px);
  }
}
.myTable {
  height: 100px;
  overflow: hidden;
}
.box {
  height: 50px;
  text-align: center;
  margin-top: 20px;
}
</style>